<template>
  <div class="childFl-container">
    <div class="childFl-model">
      <div id="mymodel"></div>

      <button @click="stopAnimation">暂停或恢复</button>
      <button @click="$router.push('clickModel')">可点击弹出信息框机械臂</button>
    </div>
  </div>
</template>

<script>
import * as THREE from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader';
var mixer = null;
var mid = null;
export default {
  name: 'childFl',
  data() {
    return {
      status: 1,
      camera: null, // 相机
      scene: null, // 场景
      renderer: null, // 渲染器
      loader: null, // 加载
      controls: null, // 控制器
      mesh: null,
      list: [],
      clock: null,
      datas: {
        oee: 100,
      },
      timer: null,
    };
  },
  created() {},
  mounted() {
    this.init();
    this.render();
    this.onWindowResize();
    window.addEventListener('resize', () => {
      this.onWindowResize();
    });
  },
  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    // 模型初始化
    initData() {},
    init() {
      this.createScene(); // 创建场景
      this.loadSTL(); // 加载STL模型
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.animate(); // 动画
      this.createGrid(); // 创建地表格
    },
    onWindowResize() {
      const element = document.getElementById('mymodel');
      this.camera.aspect = element.clientWidth / element.clientHeight;
      this.camera.updateProjectionMatrix();
      this.renderer.setSize(element.clientWidth, element.clientHeight);
    },
    // 创建辅助线
    wire() {
      var axisHelper = new THREE.AxesHelper(500);
      this.scene.add(axisHelper);
    },
    // 模型初始化
    // 创建场景
    createScene() {
      this.scene = new THREE.Scene();
    },
    // 加载STL模型
    loadSTL() {
      let this_ = this;
      const loader = new GLTFLoader();
      const dracoLoader = new DRACOLoader();
      loader.setDRACOLoader(dracoLoader);
      loader.load('/model/roboticArm.gltf', function (gltf) {
        gltf.scene.scale.set(1, 1, 1);
        gltf.scene.rotation.set(0, Math.PI / 2, 0);
        mixer = new THREE.AnimationMixer(gltf.scene);
        mixer.clipAction(gltf.animations[0]).play();
        this_.scene.add(gltf.scene);
      });
    },
    // 创建光源
    createLight() {
      this.clock = new THREE.Clock();
      // // 环境光
      this.scene.add(new THREE.AmbientLight(0x444444));
      const light = new THREE.DirectionalLight(0xffffff); // 创建环境光
      light.position.set(0, 20, 20);
      light.shadow.camera.top = 10;
      light.shadow.camera.bottom = -10;
      light.shadow.camera.left = -10;
      light.shadow.camera.right = 10;
      light.castShow = true;
      this.scene.add(light); // 将光源添加到场景中
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById('mymodel');

      const width = element.clientWidth; // 窗口宽度
      const height = element.clientHeight; // 窗口高度
      const k = width / height; // 窗口宽高比
      this.scene.background = new THREE.Color(0xbbbbbb);
      this.camera = new THREE.PerspectiveCamera(45, k, 1, 10000); // 看到的角度范围 45,  渲染窗口的长宽比,  距离相机多远的位置开始渲染，  距离相机多远的位置截止渲染,值偏小，会有部分场景看不到
      this.camera.position.set(20, 70, 170); // 设置相机位置
      this.scene.add(this.camera); // 将相机添加到场景中
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById('mymodel');
      this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); // 是否开启反锯齿, 是否可以设置背景色透明
      this.renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      this.renderer.shadowMap.enabled = true; // 显示阴影
      this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;

      element.appendChild(this.renderer.domElement);
    },
    createGrid() {
      // 地表格
      const grid = new THREE.GridHelper(500, 100, 0xffffff, 0xffffff);
      grid.material.opacity = 0.5;
      grid.material.depthWrite = false;
      grid.material.transparent = true;

      const axesHelper = new THREE.AxesHelper(40);
      this.scene.add(grid, axesHelper);
    },
    render() {
      this.renderer.render(this.scene, this.camera);
    },
    // 创建控件对象
    createControls() {
      this.controls = new OrbitControls(this.camera, this.renderer.domElement); // OrbitControls 支持鼠标左中右操作
    },
    animate() {
      mid = requestAnimationFrame(this.animate);
      if (mixer) mixer.update(this.clock.getDelta());
      this.controls.update(); // required if damping enabled
      this.render();
    },
    // 暂停动画
    stopAnimation() {
      if (mid) {
        cancelAnimationFrame(mid);
        mid = null;
      } else {
        requestAnimationFrame(this.animate);
      }
    },
  },
};
</script>

<style>
html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
.childFl-container {
  width: 100%;
  height: 100%;
}

.childFl-model,
#mymodel {
  width: 100%;
  height: 100%;
}

.childFl-model {
  position: relative;
}
button {
  position: absolute;
  top: 20px;
  width: 100px;
  height: 50px;
  cursor: pointer;
  background: #fff;
}

button:last-of-type {
  width: 150px;
  left: 60%;
}
</style>